//
//  CachetDetailViewController.m
//  云律司
//
//  Created by 韦淋 on 2017/9/12.
//  Copyright © 2017年 test. All rights reserved.
//

#import "CachetDetailViewController.h"

@interface CachetDetailViewController () <UIScrollViewDelegate,UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@end

@implementation CachetDetailViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = false;
    self.barTitleLable.text = @"公章申请修改";
    self.dataSource = [NSArray array];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initUI];
}
#pragma mark - Button Target
- (void)action_CheckPic:(UIButton *)sender{
    NSLog(@"查看盖章图片 ");
}
#pragma mark - Private Method
- (void)getData{
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@create_id=%@",SEALALTERLIST_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"]] dic:nil Succeed:^(id responseData) {
        NSLog(@"responseData = %@",responseData);
        self.dataSource = responseData[@"result"];
        if (self.dataSource.count == 0) {
            [MBProgressHUD showSuccess:@"暂无更多数据"];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
- (void)loadNewData{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.dataSource = nil;
        [self getData];
    });
    
    
}
- (void)initUI{
    [self.view addSubview:self.tableView];
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 205;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AlterCachetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"alterCachetCell" forIndexPath:indexPath ];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLable.text = @"***.doc";
    cell.receiveLable.text = @"1";
    cell.documentType.text = @"2";
    cell.approveState.text = @"3";
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 205)];
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 205)];
    backView.backgroundColor = [UIColor whiteColor];
    [header addSubview:backView];
    
    for (int i = 0; i < self.titleArray.count; i ++) {
        
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0 + i * 40, self.view.bounds.size.width - 20, 40)];
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.layer.borderWidth = 1.0f;
        bgView.layer.borderColor = [[UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f] CGColor];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 40, 40)];
        view.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
        
        UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 90, 40)];
        titleLable.textColor = [UIColor blackColor];
        titleLable.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
        titleLable.font = [UIFont systemFontOfSize:13];
        titleLable.text = self.titleArray[i];
        
        UILabel *redStar = [[UILabel alloc] initWithFrame:CGRectMake(130, 0, 10, 40)];
        redStar.textColor = [UIColor redColor];
        redStar.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
        redStar.font = [UIFont systemFontOfSize:13];
        
        [backView addSubview:bgView];
        
        [bgView addSubview:titleLable];
        [bgView addSubview:redStar];
        
        UITextField *inputInfo = [[UITextField alloc] initWithFrame:CGRectMake(160, 0 + i * 40, 200, 40)];
        inputInfo.clearButtonMode = UITextFieldViewModeWhileEditing;
        inputInfo.textAlignment = NSTextAlignmentLeft;
        inputInfo.font = [UIFont systemFontOfSize:13];
        inputInfo.delegate = self;
        inputInfo.tag = i + 100;
        
        UIImageView *titleImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
        titleImage.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
        titleImage.image = [UIImage imageNamed:self.imageArray[i]];
        [bgView addSubview:titleImage];
        [backView addSubview:inputInfo];
        [bgView insertSubview:view belowSubview:titleImage];
    }
    
    
    return header;
}
#pragma mark - Getter
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT - 70) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 180;
        [_tableView registerClass:[AlterCachetCell class] forCellReuseIdentifier:@"alterCachetCell"];
        
        //下拉刷新
        
        NSMutableArray *images = [NSMutableArray array];//图片
        for (int i = 1; i < 48; i++) {//假设50张图片
            [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%zd",i]]];
        }
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        //        header.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
        //        [header setImages:images forState:MJRefreshStateRefreshing];// 设置正在刷新状态的动画图片
        
        // 设置普通状态的动画图片
        [header setImages:@[images[0]] forState:MJRefreshStateIdle];
        // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
        [header setImages:@[images[0]] forState:MJRefreshStatePulling];
        // 设置正在刷新状态的动画图片
        [header setImages:images forState:MJRefreshStateRefreshing];
        _tableView.mj_header = header;// 设置header
        header.lastUpdatedTimeLabel.hidden = YES;// 隐藏时间
        header.stateLabel.hidden = YES;// 隐藏状态
    }
    return _tableView;
}

- (NSArray *)titleArray{
    if (!_titleArray) {
        
        _titleArray = [NSArray arrayWithObjects:@"相 关 案 件  ：",@"案      号  ：",@"委    托    人  ：",@"当    事    人  ：",@"对方当事人  ：",nil];
        
    }
    return _titleArray;
}
- (NSArray *)imageArray{
    if (!_imageArray) {
        
        _imageArray = [NSArray arrayWithObjects:@"llist_01",@"llist_02",@"llist_03",@"llist_0",@"llist_05",nil];
    }
    return _imageArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
